/* c_followup_rates.do -******************************************************
	This program produces Data Appendix Table B3
******************************************************************************/
set more off
local grades "K1"
local years "1997 1998 1999 2000 2001 2002 2003"
foreach g of local grades {

matrix reg_result = J(16, 6,-1)
matrix colnames reg_result = year obs cntrl_find_prim_samp find_prim_samp cntrl_fin_samp
local i =1
foreach y of local years {

	*Merge in NSC that gets rid of duplicate and siblings and such
	use "$stata_data_analysis/nsc/nsc_ready_fin.dta", clear
	keep if gr_asgn == "`g'" & yr_asgn == `y'

	*Merge in SASID data
	merge 1:1 studentno using "$stata_data_sims/SIMS_wide.dta", keep(master match)
	gen has_sasid = _merge == 3
	drop _merge

	keep if !inlist(pformany,0,1)

	matrix reg_result[`i',1] = `y'
	count
	matrix reg_result[`i'+1,2] = r(N)

	*Loop over vars of interest to find to fill in the table.
	local vars find_prim_name has_sasid
	local j= 2
	foreach var of local vars{
		replace `var' = 0 if `var' == .
		su `var' if soffany == 0
		matrix reg_result[`i', `j++'] = r(mean)

		reg `var' soffany, absorb(pformany) robust
		matrix reg_result[`i', `j'] = _b[soffany]
		matrix reg_result[`i'+1, `j++'] = _se[soffany]


	}
	di `i++'
	di `i++'
}
clear

*Load in the primary named data set
use "$stata_data_analysis/nsc/nsc_ready_fin.dta", clear

*Merge in SASID data
merge 1:1 studentno using "$stata_data_sims/SIMS_wide.dta", keep(master match)
gen has_sasid = _merge == 3
drop _merge


keep if !inlist(pformany,0,1) & gr_asgn == "K1"

matrix reg_result[15,1] = 0
count
matrix reg_result[16,2] = r(N)

local vars find_prim_name has_sasid
local j= 2
foreach var of local vars{
	replace `var' = 0 if `var' == .

	su `var' if soffany == 0
	matrix reg_result[15, `j++'] = r(mean)

	reg `var' soffany i.yr_asgn, absorb(pformany) robust
	matrix reg_result[15, `j'] = _b[soffany]
	matrix reg_result[16, `j++'] = _se[soffany]
}

matrix reg_result[15, `j++'] = r(mean)

* Contents of table_B3.xlsx can be pasted into cell J6 of sheet B3_att_reg of the Excel deck
putexcel set "$results/raw_tabs/table_b3", replace
putexcel A1 = matrix(reg_result)
matrix list reg_result
}
